/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufc.quixada.adrs.dao;

import br.ufc.quixada.adrs.dao.config.PostGresMapConfig;
import br.ufc.quixada.adrs.model.Adrs;
import br.ufc.quixada.adrs.model.Contato;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Caio
 */
public class AdrsDAO {

    public synchronized void insert(Adrs adrs) throws SQLException {
        Long l = (Long) PostGresMapConfig.getSqlMapClient().queryForObject("getMaxIdUsuario");
        if (l == null) {
            l = 0L;
        }
        adrs.getUsuario().setId(l + 1);

        l = (Long) PostGresMapConfig.getSqlMapClient().queryForObject("getMaxIdEndereco");
        if (l == null) {
            l = 0L;
        }
        adrs.getUsuario().getEndereco().setId(l + 1);

        Long id = (Long) PostGresMapConfig.getSqlMapClient().queryForObject("getMaxIdContato");
        if (id == null) {
            id = 0L;
        }
        for (Contato d : adrs.getUsuario().getContatos()) {
            d.setId(++id);
        }

        id = getProxId();
        adrs.setId(id);
        PostGresMapConfig.getSqlMapClient().insert("addAdrs", adrs);
    }

    public synchronized void update(Adrs adrs) throws SQLException {
        Long id = (Long) PostGresMapConfig.getSqlMapClient().queryForObject("getMaxIdContato");
        if (id == null) {
            id = 0L;
        }
        for (Contato d : adrs.getUsuario().getContatos()) {
            d.setId(++id);
        }
        PostGresMapConfig.getSqlMapClient().update("updateAdrs", adrs);
    }

    public void delete(Adrs adrs) throws SQLException {
        PostGresMapConfig.getSqlMapClient().update("deleteAdrs", adrs);
    }

    public void reativarAdrs(Adrs adrs) throws SQLException {
        PostGresMapConfig.getSqlMapClient().update("reativarContaAdrs", adrs);
    }

    public Adrs getByUsuarioId(Long id) throws SQLException {
        return (Adrs) PostGresMapConfig.getSqlMapClient().queryForObject("getAdrsByUsuarioId", id);
    }

    public Adrs getByUsuarioIdToAtivarConta(Long id) throws SQLException {
        return (Adrs) PostGresMapConfig.getSqlMapClient().queryForObject("getAdrsByUsuarioIdToAtivarConta", id);
    }

    public List<Adrs> getAll() throws SQLException {
        return (List<Adrs>) PostGresMapConfig.getSqlMapClient().queryForList("getAllAdrs");
    }

    public List<Adrs> getPaginacaoADRS(Map dadosPaginacao) throws SQLException {
        return (List<Adrs>) PostGresMapConfig.getSqlMapClient().queryForList("getAdrsPaginacao", dadosPaginacao);
    }

    public List<Adrs> getPaginacaoByADRSLetraInicial(Map dadosPaginacao) throws SQLException {
        return (List<Adrs>) PostGresMapConfig.getSqlMapClient().queryForList("getAdrsByLetraInicial", dadosPaginacao);
    }

    public List<Adrs> getByMunicipioId(Long id) throws SQLException {
        return (List<Adrs>) PostGresMapConfig.getSqlMapClient().queryForList("getAdrsByMunicipioId", id);
    }

    public List<Adrs> getByEstadoId(Long id) throws SQLException {
        return (List<Adrs>) PostGresMapConfig.getSqlMapClient().queryForList("getAdrsByEstadoId", id);
    }

    public Adrs getById(Long id) throws SQLException {
        return (Adrs) PostGresMapConfig.getSqlMapClient().queryForObject("getAdrsById", id);
    }

    public List<Adrs> getByPrazoId(Long prazoId) throws SQLException {
        return (List<Adrs>) PostGresMapConfig.getSqlMapClient().queryForList("getAdrsByPrazoId", prazoId);
    }
    
    public List<Adrs> getConfirmadosByPrazoId(Long prazoId) throws SQLException {
        return (List<Adrs>) PostGresMapConfig.getSqlMapClient().queryForList("getConfirmadosByPrazoId", prazoId);
    }
    
    public List<Adrs> getNaoConfirmadosByPrazoId(Long prazoId) throws SQLException {
        return (List<Adrs>) PostGresMapConfig.getSqlMapClient().queryForList("getNaoConfirmadosByPrazoId", prazoId);
    }

    public Long countNumeroAdrs() throws SQLException {
        Long contador = (Long) PostGresMapConfig.getSqlMapClient().queryForObject("countNumeroAdrs");
        if (contador == null) {
            contador = 0L;
        }
        return contador;
    }

    private Long getProxId() throws SQLException {
        Long id = (Long) PostGresMapConfig.getSqlMapClient().queryForObject("getMaxIdAdrs");
        if (id == null) {
            id = 0L;
        }
        return id + 1L;
    }

    public List<Adrs> searchByNome(String nome) throws SQLException {
        return (List<Adrs>) PostGresMapConfig.getSqlMapClient().queryForList("getAdrsByNome", nome);
    }

    public List<Adrs> getNaoIncluidosByPrazoId(Long prazoId) throws SQLException {
        return (List<Adrs>) PostGresMapConfig.getSqlMapClient().queryForList("getAdrsNaoIncluidosByPrazoId", prazoId);
    }
    
    public List<Adrs> getExcluidos(Long adminId) throws SQLException {
        return (List<Adrs>) PostGresMapConfig.getSqlMapClient().queryForList("getAdrsExcluidos", adminId);
    }
}
